package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.InventoryAlarmNoticeDao;
import com.wing.cabinet.model.entity.InventoryAlarmNotice;
import com.wing.cabinet.model.request.InventoryAlarmNoticeForm;
import com.wing.cabinet.model.response.CellSingleInventoryProductVO;
import com.wing.cabinet.model.response.InventoryAlarmNoticeVO;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.Result;
import com.wing.setting.model.entity.SystemRuleInfo;
import com.wing.setting.service.SystemRuleInfoService;
import com.wing.sms.feign.SmsClient;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.UserVO;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 库存告警通知-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class InventoryAlarmNoticeServiceImpl extends ServiceImpl<InventoryAlarmNoticeDao, InventoryAlarmNotice> implements InventoryAlarmNoticeService {

    @Autowired
    private  CellSingleInventoryProductService cellSingleInventoryProductService;

    @Autowired
    private SmsClient smsClient;

    @Autowired
    private SystemRuleInfoService systemRuleInfoService;

    @Autowired
    private UserClient userClient;

    @Override
    public int add(InventoryAlarmNoticeForm inventoryAlarmNoticeForm) {
        //校验
        InventoryAlarmNotice inventoryAlarmNotice =new InventoryAlarmNotice();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(InventoryAlarmNotice.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(inventoryAlarmNoticeForm, inventoryAlarmNotice, copyOptions);

        return baseMapper.insert(inventoryAlarmNotice);
    }

    @Override
    public List<InventoryAlarmNoticeVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<InventoryAlarmNoticeVO> pageList4app(Page page, Map<String, Object> params) {
        List<InventoryAlarmNoticeVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public InventoryAlarmNoticeVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public void sendNotification() {
        String code = "";
        List<String> oldCodeList = new ArrayList<>();
        List<String> newCodeList = new ArrayList<>();
        List<SystemRuleInfo> list = systemRuleInfoService.list();
        if(StringUtils.isNotBlank(list.get(0).getInventoryManageUserId())){
            List<CellSingleInventoryProductVO> alarmInformation = cellSingleInventoryProductService.getAlarmInformation();
            for(CellSingleInventoryProductVO item:alarmInformation){
                InventoryAlarmNotice inventoryAlarmNotice = baseMapper.selectOne(new LambdaQueryWrapper<InventoryAlarmNotice>()
                        .eq(InventoryAlarmNotice::getSkuCode, item.getSkuCode())
                        .eq(InventoryAlarmNotice::getEnterpriseCode,JwtUtils.getEnterpriseCode()));
                if(inventoryAlarmNotice!=null){
                    oldCodeList.add(item.getSkuCode());
                }else {
                    newCodeList.add(item.getSkuCode());
                    InventoryAlarmNotice alarmNotice = new InventoryAlarmNotice();
                    alarmNotice.setBelongCabinet(item.getBelongCabinet());
                    alarmNotice.setQuantity(item.getQuantity());
                    alarmNotice.setQuantitySetting(item.getQuantitySetting());
                    alarmNotice.setSkuCode(item.getSkuCode());
                    alarmNotice.setSkuSpecOld(item.getSkuSpecOld());
                    alarmNotice.setEnterpriseCode(JwtUtils.getEnterpriseCode());
                    baseMapper.insert(alarmNotice);
                }
            }
            if(newCodeList.size()>3){
                code = newCodeList.stream().limit(3).collect(Collectors.toList()).stream().collect(Collectors.joining(","));
            }else if(oldCodeList.size()>3){
                Collections.reverse(oldCodeList);
                code = oldCodeList.stream().limit(3).collect(Collectors.toList()).stream().collect(Collectors.joining(","));
            }else {
                List<String> stringList = Stream.of(newCodeList, oldCodeList).flatMap(x -> x.stream()).distinct().collect(Collectors.toList());
                code = stringList.stream().limit(3).collect(Collectors.toList()).stream().collect(Collectors.joining(","));
            }
            String[] strId = list.get(0).getInventoryManageUserId().split(",");
            for (String item:strId){
                JsonResult<UserVO> userInfo = userClient.getUserInfo(Long.valueOf(item));
                if(StringUtils.isNotBlank(userInfo.getData().getPhone())){
                    smsClient.sendAlarmNoticeCode(userInfo.getData().getPhone(), code);
                }
            }
        }
    }

}
